package cn.edu.hit.education.cellmodifier;

import java.util.List;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import cn.edu.hit.education.pojo.ExCourse;
import cn.edu.hit.education.pojo.Schedule;
import cn.edu.hit.education.view.CourseInformationView;

public class CourseInformationCellModifier implements ICellModifier {
	private CourseInformationView view;
	private TableViewer tv;
	public static String[] OPENS = {"不开","部分","开课"};
	public static String[] SEMESTERS = {"Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ"};

	public CourseInformationCellModifier(CourseInformationView view,TableViewer tv){
		this.view = view;
		this.tv = tv;
	}
	
	@Override
	public boolean canModify(Object element, String property) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public Object getValue(Object element, String property) {
		// TODO Auto-generated method stub
		ExCourse course = (ExCourse)element;
		
		if(property.equals("number")){
			return course.getNumber();
		}else if(property.equals("name")){
			return course.getName();
		}else if(property.equals("credicts")){
			return  String.valueOf(course.getCredits());
		}else if(property.equals("period")){
			return  String.valueOf(course.getPeriod());
		}else if(property.equals("theoretical")){
			return  String.valueOf(course.getTheoretical());
		}else if(property.equals("experiment")){
			return  String.valueOf(course.getExperiment());
		}
		throw new RuntimeException("error colum name : " + property);
	}

	 private int getNameIndex(String name){
         for(int i=0;i<SEMESTERS.length;i++){
             if(SEMESTERS[i].equals(name)){
                 return i;
             }
         }
         return -1;
     }
	 
	@Override
	public void modify(Object element, String property, Object value) {
		// TODO Auto-generated method stub
		TableItem item = (TableItem)element;
		ExCourse course = (ExCourse)item.getData();
		List<Schedule> schedules = course.getSchedules();
		
		if(property.equals("number")){
			course.setNumber((String)value);
		}else if(property.equals("name")){
			course.setName((String)value);
		}else if(property.equals("credicts")){
			course.setCredits(Double.parseDouble((String)value));
		}else if(property.equals("period")){
			course.setPeriod(Double.parseDouble((String)value));
		}else if(property.equals("theoretical")){
			course.setTheoretical(Double.parseDouble((String)value));
		}else if(property.equals("experiment")){
			course.setExperiment(Double.parseDouble((String)value));
		}else{
            throw new RuntimeException("错误列名:" + property);
        }
		
		tv.update(course, null);
		
		view.updateDatabase(course);
	}

}
